package com.example.neptune.recyclerview_109zj.model;

import com.example.neptune.recyclerview_109zj.bean.ArticleBean;
import com.example.neptune.recyclerview_109zj.iface.ArticleListener;
import com.example.neptune.recyclerview_109zj.iface.Articleiface;
import com.example.neptune.recyclerview_109zj.service.ArticleService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Neptune on 2017/4/19.
 */
//model,文章模块，实现Articleiface
public class ArticleModel implements Articleiface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";

    //构造方法
public ArticleModel()
{
  retrofit=new Retrofit.Builder()
          .baseUrl(BASEURL)
          .addConverterFactory(GsonConverterFactory.create())
          .build();      //网络
}
    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener listener) {
        ArticleService articleService=retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call;
        call = articleService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body());
                else
                    listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t){
                listener.onFail(t.toString());
            }
        });
    }
}
